package com.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.common.tool.HttpRequest;
import com.common.tool.WeiXinMenuUtil;
import com.common.tool.WeiXinUtil;
import com.dao.MemberAddress;
import com.dao.MemberBuy;
import com.dao.MemberShopReq;
import com.dao.WeixinUserinfo;
import com.entity.BaseMemberBuy;
import com.entity.CommonConstant;
import com.entity.weixin.TextMessage;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

public class JsonController extends Controller {

    private JSONObject json = null;
    private boolean flag = false;
    private String msg = "";

    public void deleteAddr() {
	// String openId= this.getSessionAttr("openId").toString();
	String addressId = this.getPara("addressId").toString();
	MemberAddress.dao.deleteById(addressId);
	renderJson("成功删除收货地址！");
    }

    public void setAddrDefault() {
	JSONObject json = new JSONObject();
	boolean flag = false;
	String msg = "";
	try {
	    // String openId= this.getSessionAttr("openId").toString();
	    String openId = "";
	    String addressId = this.getPara("addressId").toString();
	    String sql = "update member_address set isdefault=1 where openid ='" + openId + "' and addressid="
		    + addressId;
	    Db.update(sql);
	    sql = "update member_address set isdefault=0 where openid ='" + openId + "' and addressid!=" + addressId;
	    Db.update(sql);
	    msg = "成功设置为默认收货地址！";
	    flag = true;
	} catch (Exception e) {
	    msg = e.getMessage();
	}
	json.put("msg", msg);
	json.put("flag", flag);
	renderJson(json);
    }

    public void queryOwerAddress() {
	json = new JSONObject();
	String openId = this.getPara("openId");
	int addressLen = 0;
	try {
	    // String openId= this.getSessionAttr("openId").toString();
	    String sql = "select * from member_address where openid ='" + openId + "'";
	    List<MemberAddress> addresslst = MemberAddress.dao.find(sql);
	    if (addresslst != null && addresslst.size() > 0) {
		msg = "成功查找到我的收货地址！";
		addressLen = addresslst.size();
		json.put("addresslst", addresslst);
	    }
	    flag = true;
	} catch (Exception e) {
	    msg = e.getMessage();
	}
	json.put("msg", msg);
	json.put("flag", flag);
	json.put("addressLen", addressLen);
	renderJson(json);
    }

    public void queryDefaultAddress() {
	String openId = "";
	// String openId= this.getSessionAttr("openId").toString();
	String sql = "select * from member_address  where openid ='" + openId + "' and isdefault=1";
	json = queryAddressOne(sql);
	renderJson(json);
    }

    public void queryFirstAddress() {
	String openId = "";
	// String openId= this.getSessionAttr("openId").toString();
	String sql = "select * from member_address  where openid ='" + openId + "'";
	json = queryAddressOne(sql);
	renderJson(json);
    }

    public void showAddressOne() {
	json = new JSONObject();
	String openId = this.getPara("openId");
	// String openId= this.getSessionAttr("openId").toString();
	try {
	    String sql = "select * from member_address  where openid ='" + openId + "' and isdefault=1";
	    MemberAddress address = MemberAddress.dao.findFirst(sql);

	    if (address == null) {
		sql = "select * from member_address  where openid ='" + openId + "'";
		address = MemberAddress.dao.findFirst(sql);
	    }
	    if (address != null) {
		msg = "成功查找到我的收货地址！";
		json.put("address", address);
	    }
	    flag = true;
	} catch (Exception e) {
	    msg = e.getMessage();
	}
	json.put("msg", msg);
	json.put("flag", flag);
	renderJson(json);
    }

    private JSONObject queryAddressOne(String sql) {
	JSONObject json = new JSONObject();
	try {
	    MemberAddress address = MemberAddress.dao.findFirst(sql);
	    if (address != null) {
		msg = "成功查找到我的收货地址！";
		json.put("address", address);
	    }
	    flag = true;
	} catch (Exception e) {
	    msg = e.getMessage();
	}
	json.put("msg", msg);
	json.put("flag", flag);
	return json;
    }

    public void buy() {
	JSONObject json = new JSONObject();
	try {
	    MemberBuy memberBuy = new MemberBuy();
	    memberBuy.setOpenid(this.getPara("openId"));
	    memberBuy.setAddressinfo(this.getPara("address"));
	    memberBuy.setUsername(this.getPara("username"));
	    memberBuy.setUserphone(this.getPara("userphone"));
	    memberBuy.setUnitprice(Double.valueOf(this.getPara("unitprice")));
	    memberBuy.setCategoryName(this.getPara("categoryName"));
	    memberBuy.setMoney(Double.valueOf(this.getPara("money")));
	    memberBuy.setNum(Integer.valueOf(this.getPara("num")));
	    memberBuy.setRemark(this.getPara("remark"));
	    memberBuy.setCategoryid(Long.valueOf(1));
	    String seq = null;
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
	    Date date = new Date();
	    seq = sdf.format(date);
	    memberBuy.setOrdercode(seq);
	    memberBuy.setBuystate(CommonConstant.VALIDATESATE);
	    memberBuy.setPaystate(CommonConstant.INVALIDATESATE);
	    memberBuy.save();
	    System.out.println("memberBuy.getBuyid()::::" + memberBuy.getBuyid());
	    json.put("buyid", memberBuy.getBuyid());
	    msg = "成功购买，你的货将飞奔而来，请您静等佳音！";
	    flag = true;
	} catch (Exception e) {
	    msg = e.getMessage();
	}
	json.put("msg", msg);
	json.put("flag", flag);
	this.renderJson(json);
    }

    /*
     * 会员申请
     */
    public void memberupreq() {
	JSONObject json = new JSONObject();
	String openId = this.getSessionAttr("openId").toString();
	try {
	    MemberShopReq memberShopReq = new MemberShopReq();
	    memberShopReq.setOpenid(openId);
	    memberShopReq.save();
	    msg = "申请信息已成功发送，请您静等佳音！";
	    flag = true;
	    WeiXinUtil weixinUtil = new WeiXinUtil();
	    String sql = "select * from weixin_userinfo where openid='" + openId + "'";
	    WeixinUserinfo userinfo = WeixinUserinfo.dao.findFirst(sql);
	    String userName = userinfo.getNickname();
	    weixinUtil.weixinSendMemberReqMessge(userName, openId);
	} catch (Exception e) {
	    msg = e.getMessage();
	}
	json.put("msg", msg);
	json.put("flag", flag);
	this.renderJson(json);
    }

    // 升级为商铺审批
    public void memberupaudit() {
	JSONObject json = new JSONObject();
	try {
	    String reqopenId = this.getPara("reqopenId");
	    String state = this.getPara("state");

	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");

	    Date date = new Date();
	    String seq = sdf.format(date);

	    StringBuilder sql = new StringBuilder("update member_shop_req ").append(" set reqstate= ").append(state)
		    .append(" , confirmdate= '").append(seq).append("'").append(" where openid='").append(reqopenId)
		    .append("'");
	    Db.update(sql.toString());

	    sql = new StringBuilder("update weixin_subscribe set membertypecode=30 where fromusername='")
		    .append(reqopenId).append("'");
	    Db.update(sql.toString());

	    if (String.valueOf(CommonConstant.VALIDATESATE).equals(state)) {
		msg = "审核通过,会员升级为商铺";
	    } else {
		msg = "审核不通过,会员不能升级为商铺";
	    }
	    flag = true;

	} catch (Exception e) {
	    msg = e.getMessage();
	}
	json.put("msg", msg);
	json.put("flag", flag);
	System.out.println(msg);
	this.renderJson(json);
    }

    /**
     * 修改支付状态 @Title: updatePayState @Description: TODO(这里用一句话描述这个方法的作用) @param
     * 设定文件 @return void 返回类型 @throws
     */
    public void updatePayState() {
	JSONObject json = new JSONObject();
	try {
	    String payState = this.getPara("paystate");
	    String buyId = this.getPara("buyid");
	    String sql = "update member_buy set paystate =" + payState + " where buyid= '" + buyId + "'";
	    Db.update(sql);
	    flag = true;
	} catch (Exception e) {
	    msg = e.getMessage();
	}
	json.put("msg", msg);
	json.put("flag", flag);
	this.renderJson(json);
    }
    // TODO:审批通过后，自身变为商铺；
    // TODO:查找商铺关系表中是否自身之前是否是其他商铺的下级；如果是，删除本身以及其下级的商铺关系；
    // TODO:在商铺关系表中添加自身的商铺以及下属的商铺关系；
    // TODO:新会员关注时，如果自己上级是其商铺下级，在商铺关系中添加其商铺关系

}
